package com.mayikt.service;

import com.mayikt.base.BaseApiService;
import com.mayikt.base.BaseResponse;
import com.mayikt.entity.IntegralEntity;
import com.mayikt.mapper.IntegralMapper;
import com.mayikt.service.req.dto.IntegralReqDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IntegralServiceImpl extends BaseApiService implements IntegralService {
    @Autowired
    private IntegralMapper integralMapper;
    @Override
    public BaseResponse<String> addIntegral(IntegralReqDto integralReqDto) {
        // 1.验证参数
        Long userId = integralReqDto.getUserId();
        if(userId==null){
            return  setResultError("userId不能为空!");
        }
        String paymentId = integralReqDto.getPaymentId();
        if(StringUtils.isEmpty(paymentId)){
            return  setResultError("paymentId不能为空!");
        }
        Long integral = integralReqDto.getIntegral();
        if(integral==null){
            return  setResultError("integral不能为空!");
        }
        // 2.dto转换do
        IntegralEntity integralEntity = dtoToDo(integralReqDto, IntegralEntity.class);
        // 插入到数据库中  根据支付id实现唯一约束，无法重复插入，所以不会重复增加积分。
        int insertIntegral = integralMapper.insertIntegral(integralEntity);
        if(insertIntegral<=0){
            // 将该日志信息记录下来，后期采用定时任务实现补偿
            return  setResultError("增加积分失败");
        }
        return setResultSuccess("增加积分成功");
    }
}
